package com.community.util;

public class NumberUtil {

    /**
     * 转为int
     * @param o
     * @return
     */
    public static int safeToNumber(Object o){
        Integer r = 0;
        if (o != null) {
            try {
                r = new Integer(String.valueOf(o));
            } catch (Exception var4) {
            }
        }

        return r;
    }

    /**
     * 转double
     * @param o
     * @return
     */
    public static Double safeToDouble(Object o){
        Double d = 0.0;
        if(o != null){
            try{
                d = new Double(StringUtil.valueOfString(o));
            }catch (Exception e){

            }
        }
        return d;
    }


    /**
     * 转double
     * @param o
     * @return
     */
    public static Double safeToDouble(Object o,Double defaultNum){
        Double d = 0.0;
        if(o != null){
            try{
                d = new Double(StringUtil.valueOfString(o));
            }catch (Exception e){

            }
            if(d == 0.0){
                return defaultNum;
            }
        }
        return d;
    }


    /**
     * 转数字 如为空则为默认值
     * @param o
     * @param defaultNum
     * @return
     */
    public static int safeToNumber(Object o,Integer defaultNum){
        Integer r = 0;
        if (o != null) {
            try {
                r = new Integer(String.valueOf(o));
            } catch (Exception var4) {
            }
        }
        if(r  == 0){
            return defaultNum;
        }
        return r;
    }
}
